package com.app.business.global.thirdservice.icc.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.app.business.global.thirdservice.icc.dto.req.*;
import com.app.business.global.thirdservice.icc.dto.res.*;
import com.app.common.dto.res.PageRes;
import com.app.common.util.Assert;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.IClient;
import com.dahuatech.icc.oauth.http.IccHttpHttpRequest;
import com.dahuatech.icc.oauth.profile.IccProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class IccServiceImpl implements IccService {
    @Autowired
    @Qualifier("iccDefaultClient")
    private IClient iClient;
    
    @Override
    public IccResult<PageRes<PageEquipmentChannelRes>> pageEquipmentChannel(PageEquipmentChannelReq req) throws ClientException {
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/evo-brm/1.2.0/device/channel/subsystem/page", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(req));
        
        // 获取响应
        JSONObject jsonObject = JSONUtil.parseObj(iClient.doAction(iccHttpHttpRequest));
        
        // 拼接响应结果
        IccResult iccResult = new IccResult();
        iccResult.setSuccess(jsonObject.get("success", Boolean.class));
        iccResult.setCode(jsonObject.get("code", Integer.class));
        iccResult.setDesc(jsonObject.get("desc", String.class));
        
        JSONObject dataObject = jsonObject.getJSONObject("data");
        PageRes pageRes = null;
        if (dataObject != null) {
            List<PageEquipmentChannelRes> rowsList = new ArrayList<>();
            JSONArray rowsArray = dataObject.getJSONArray("pageData");
            if (rowsArray != null && rowsArray.size() != 0) {
                for (int i = 0; i < rowsArray.size(); i++) {
                    JSONObject recordObj = rowsArray.getJSONObject(i);
                    rowsList.add(recordObj.toBean(PageEquipmentChannelRes.class));
                }
            }
            
            pageRes = new PageRes();
            pageRes.setTotal(dataObject.get("totalRows", Long.class));
            pageRes.setPageSize(dataObject.get("pageSize", Integer.class));
            pageRes.setCurrentPage(dataObject.get("currentPage", Integer.class));
            pageRes.setRows(rowsList);
        }
        iccResult.setData(pageRes);
        return iccResult;
    }
    
    @Override
    public IccResult<GetRealMonitorRes> getRealMonitor(GetRealMonitorReq req) throws ClientException {
        // 拼接参数
        Map reqMap = new HashMap();
        reqMap.put("data", req);
        
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/admin/API/MTS/Video/StartVideo", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(reqMap));
        
        
        try {
            String responseStr = iClient.doAction(iccHttpHttpRequest);
            // 获取响应
            JSONObject jsonObject = JSONUtil.parseObj(responseStr);
            
            // 拼接响应结果
            IccResult iccResult = new IccResult();
            iccResult.setSuccess(Objects.equals(jsonObject.get("code", Integer.class), 1000));
            iccResult.setCode(jsonObject.get("code", Integer.class));
            iccResult.setDesc(jsonObject.get("desc", String.class));
            iccResult.setData(jsonObject.get("data", GetRealMonitorRes.class));
            return iccResult;
        } catch (Exception e) {
            Assert.throwServerException("实时监控信息获取失败", e);
        }
        
        return null;
    }
    
    @Override
    public IccResult<StartTalkRes> startTalk(StartTalkReq req) throws ClientException {
        // 拼接参数
        Map reqMap = new HashMap();
        reqMap.put("data", req);
        
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/admin/API/MTS/Audio/StartTalk", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(reqMap));
        
        // 获取响应
        JSONObject jsonObject = JSONUtil.parseObj(iClient.doAction(iccHttpHttpRequest));
        
        // 拼接响应结果
        IccResult iccResult = new IccResult();
        iccResult.setSuccess(Objects.equals(jsonObject.get("code", Integer.class), 1000));
        iccResult.setCode(jsonObject.get("code", Integer.class));
        iccResult.setDesc(jsonObject.get("desc", String.class));
        iccResult.setData(jsonObject.get("data", StartTalkRes.class));
        return iccResult;
    }
    
    @Override
    public IccResult stopTalk(StopTalkReq req) throws ClientException {
        // 拼接参数
        Map reqMap = new HashMap();
        reqMap.put("data", req);
        
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/admin/API/MTS/Audio/StopTalk", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(reqMap));
        
        // 获取响应
        JSONObject jsonObject = JSONUtil.parseObj(iClient.doAction(iccHttpHttpRequest));
        
        // 拼接响应结果
        IccResult iccResult = new IccResult();
        iccResult.setSuccess(Objects.equals(jsonObject.get("code", Integer.class), 1000));
        iccResult.setCode(jsonObject.get("code", Integer.class));
        iccResult.setDesc(jsonObject.get("desc", String.class));
        return iccResult;
    }
    
    @Override
    public IccResult<GetRecordStateByMonthRes> getRecordStateByMonth(GetRecordStateByMonthReq req) throws ClientException {
        // 拼接参数
        Map reqMap = new HashMap();
        reqMap.put("data", req);
        
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/admin/API/SS/Record/GetChannelMonthRecordStatus", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(reqMap));
        
        // 获取响应
        JSONObject jsonObject = JSONUtil.parseObj(iClient.doAction(iccHttpHttpRequest));
        
        // 拼接响应结果
        IccResult iccResult = new IccResult();
        iccResult.setSuccess(Objects.equals(jsonObject.get("code", Integer.class), 1000));
        iccResult.setCode(jsonObject.get("code", Integer.class));
        iccResult.setDesc(jsonObject.get("desc", String.class));
        iccResult.setData(jsonObject.get("data", GetRecordStateByMonthRes.class));
        return iccResult;
    }
    
    @Override
    public IccResult<List<ListSimpleRecordRes>> listSimpleRecord(ListSimpleRecordReq req) throws ClientException {
        // 拼接参数
        Map reqMap = new HashMap();
        reqMap.put("data", req);
        
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/admin/API/SS/Record/QueryRecords", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(reqMap));
        
        // 获取响应
        JSONObject jsonObject = JSONUtil.parseObj(iClient.doAction(iccHttpHttpRequest));
        
        // 拼接响应结果
        IccResult iccResult = new IccResult();
        iccResult.setSuccess(Objects.equals(jsonObject.get("code", Integer.class), 1000));
        iccResult.setCode(jsonObject.get("code", Integer.class));
        iccResult.setDesc(jsonObject.get("desc", String.class));
        
        List<ListSimpleRecordRes> list = new ArrayList<>();
        JSONArray recordsArray = jsonObject.getJSONArray("records");
        if (recordsArray != null && recordsArray.size() != 0) {
            for (int i = 0; i < recordsArray.size(); i++) {
                JSONObject recordObj = recordsArray.getJSONObject(i);
                list.add(recordObj.toBean(ListSimpleRecordRes.class));
            }
        }
        iccResult.setData(list);
        return iccResult;
    }
    
    @Override
    public IccResult<GetRecordByFileRes> getRecordByFile(GetRecordByFileReq req) throws ClientException {
        // 拼接参数
        Map reqMap = new HashMap();
        reqMap.put("data", req);
        
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/admin/API/SS/Playback/StartPlaybackByFile", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(reqMap));
        
        // 获取响应
        JSONObject jsonObject = JSONUtil.parseObj(iClient.doAction(iccHttpHttpRequest));
        
        // 拼接响应结果
        IccResult iccResult = new IccResult();
        iccResult.setSuccess(Objects.equals(jsonObject.get("code", Integer.class), 1000));
        iccResult.setCode(jsonObject.get("code", Integer.class));
        iccResult.setDesc(jsonObject.get("desc", String.class));
        iccResult.setData(jsonObject.get("data", GetRecordByFileRes.class));
        return iccResult;
    }
    
    @Override
    public IccResult<GetRecordByTimeRes> getRecordByTime(GetRecordByTimeReq req) throws ClientException {
        // 拼接参数
        Map reqMap = new HashMap();
        reqMap.put("data", req);
        
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/admin/API/SS/Playback/StartPlaybackByTime", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(reqMap));
        
        // 获取响应
        JSONObject jsonObject = JSONUtil.parseObj(iClient.doAction(iccHttpHttpRequest));
        
        // 拼接响应结果
        IccResult iccResult = new IccResult();
        iccResult.setSuccess(Objects.equals(jsonObject.get("code", Integer.class), 1000));
        iccResult.setCode(jsonObject.get("code", Integer.class));
        iccResult.setDesc(jsonObject.get("desc", String.class));
        iccResult.setData(jsonObject.get("data", GetRecordByTimeRes.class));
        return iccResult;
    }
    
    @Override
    public IccResult setPtzCamera(SetPtzCameraReq req) throws ClientException {
        // 拼接参数
        Map reqMap = new HashMap();
        reqMap.put("data", req);
        
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/admin/API/DMS/Ptz/OperateCamera", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(reqMap));
        
        // 获取响应
        JSONObject jsonObject = JSONUtil.parseObj(iClient.doAction(iccHttpHttpRequest));
        
        // 拼接响应结果
        IccResult iccResult = new IccResult();
        iccResult.setSuccess(Objects.equals(jsonObject.get("code", Integer.class), 1000));
        iccResult.setCode(jsonObject.get("code", Integer.class));
        iccResult.setDesc(jsonObject.get("desc", String.class));
        return iccResult;
    }
    
    @Override
    public IccResult setPtzDirection(SetPtzDirectionReq req) throws ClientException {
        // 拼接参数
        Map reqMap = new HashMap();
        reqMap.put("data", req);
        
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/admin/API/DMS/Ptz/OperateDirect", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(reqMap));
        
        // 获取响应
        JSONObject jsonObject = JSONUtil.parseObj(iClient.doAction(iccHttpHttpRequest));
        
        // 拼接响应结果
        IccResult iccResult = new IccResult();
        iccResult.setSuccess(Objects.equals(jsonObject.get("code", Integer.class), 1000));
        iccResult.setCode(jsonObject.get("code", Integer.class));
        iccResult.setDesc(jsonObject.get("desc", String.class));
        return iccResult;
    }
    
    @Override
    public IccResult setPtzPosition(SetPtzPositionReq req) throws ClientException {
        // 拼接参数
        Map reqMap = new HashMap();
        reqMap.put("data", req);
        
        // 发送请求
        IccHttpHttpRequest iccHttpHttpRequest = new IccHttpHttpRequest(IccProfile.URL_SCHEME + "/evo-apigw/admin/API/DMS/Ptz/SitPosition", Method.POST);
        iccHttpHttpRequest.body(JSONUtil.toJsonStr(reqMap));
        
        // 获取响应
        JSONObject jsonObject = JSONUtil.parseObj(iClient.doAction(iccHttpHttpRequest));
        
        // 拼接响应结果
        IccResult iccResult = new IccResult();
        iccResult.setSuccess(Objects.equals(jsonObject.get("code", Integer.class), 1000));
        iccResult.setCode(jsonObject.get("code", Integer.class));
        iccResult.setDesc(jsonObject.get("desc", String.class));
        return iccResult;
    }
}
